; fe3tsmain.asm
; FE3 Task switcher main
;
	.include "vicrom.inc"
	
	.import __SJLOAD_LOAD__
	.import __SJLOAD_RUN__
	.import __SJLOAD_SIZE__
	
	.import __ENTRY_LOAD__
	.import __DATA_RUN__
	.import __DATA_SIZE__
	
	.import init

	.segment "BOOT"

; Startup Basic program "2011 SYS prolog"
			.word	basprg
basprg:		.word	@end
			.word	2011
			.byte	$9E
			.byte	<(prolog / 1000 .mod 10) + $30
			.byte	<(prolog / 100 .mod 10) + $30
			.byte	<(prolog / 10 .mod 10) + $30
			.byte	<(prolog / 1 .mod 10) + $30
			.byte	0
@end:		.word	0


prolog:		; init SJLOAD
			ldy #<__SJLOAD_LOAD__
			lda #>__SJLOAD_LOAD__
			sty $5F				; set src start
			sta $60
			ldy #<(__SJLOAD_LOAD__+__SJLOAD_SIZE__)
			lda #>(__SJLOAD_LOAD__+__SJLOAD_SIZE__)
			sty $5A				; set src end
			sta $5B
			ldy #<(__SJLOAD_RUN__+__SJLOAD_SIZE__)
			lda #>(__SJLOAD_RUN__+__SJLOAD_SIZE__)
			sty $58				; set target end
			sta $59
			jsr BLTUC			; block move
			jsr __SJLOAD_RUN__+2

			; init Task Switcher
			ldy #<__ENTRY_LOAD__
			lda #>__ENTRY_LOAD__
			sty $5F				; set src start
			sta $60
			ldy #<__SJLOAD_LOAD__
			lda #>__SJLOAD_LOAD__
			sty $5A				; set src end
			sta $5B
			ldy #<(__DATA_RUN__+__DATA_SIZE__)
			lda #>(__DATA_RUN__+__DATA_SIZE__)
			sty $58				; set target end
			sta $59
			jsr BLTUC			; block move
			jsr init
			
			; load supervisor
cploader:	ldx #end_l-start_l-1
@cplloop:	lda start_l, x
			sta $033C, x	; to cassette buffer
			dex
			bpl @cplloop
			jmp $033C

start_l:	lda #1				; drive no @ $BA
			ldx $BA
			bne @drv_set
			ldx #8				; set default (8)		
@drv_set:	ldy #0
			jsr SETLFS
			lda #n_length
			ldx #<n_super
			ldy #>n_super
			jsr SETNAM
			lda #0				; load
			ldx $2B
			ldy $2C
			jsr LOAD
			bcs loaderr
			stx $2D				; save end address
			sty $2E
			jsr $C659			; CLR
			jsr $C533			; Relinks BASIC Program from and to any address... 
			jmp $C7AE			; BASIC Warm Start (RUN)
			
loaderr:	ldx #$1D
			jmp ($300)
end_l:

n_super:	.byte "fe3ts-sv"
	n_length = * - n_super	

	.segment "SJLOAD"
	
			.incbin "sjload$04"
	